//
//  YHTimeSubView.m
//  ZhuKuang
//
//  Created by jjkj on 17/7/11.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "YHTimeSubView.h"
#import "YHTimeSubViewCell.h"
#import "ChartsViewController.h"

@interface YHTimeSubView()<UITableViewDelegate,UITableViewDataSource>
/**列表*/
@property (weak, nonatomic) IBOutlet UITableView *subTableView;
/**关闭按钮*/
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
/**关闭按钮距离左边的距离约束*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *closeBtnLeftConstraint;
/**关闭按钮竖直剧中约束*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *closeBtnCenterConstraint;
/**标题*/
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
/**关闭按钮所在的View*/
@property (weak, nonatomic) IBOutlet UIView *closeView;
/**车辆曲线按钮*/
@property (weak, nonatomic) IBOutlet UIButton *carLineBtn;

/**背景View*/
@property (nonatomic , strong)  UIView *maskView;

/** 是否时报警*/
@property(nonatomic, assign) BOOL isSingle;
/** 详情、报警信息*/
@property(nonatomic, strong) NSArray * list;

@end

@implementation YHTimeSubView

/**
 * 赋值
 * isSingle YES 报警 NO 详情
 * list 数据
 * title 标题
 */
-(void)setType:(BOOL)isSingle withData:(NSArray *)list withTitle:(NSString *)title withIolType:(BOOL)isIol;
{
    self.isSingle = isSingle;
    self.list = list;
    
    if (self.isSingle) {
        //报警 红色 有车辆曲线按钮
        self.titleLabel.attributedText = [CommClass getTitleStr:[title stringByAppendingString:@"报警详情"] withImgName:@"alram_icon" withImgY:3];
        self.closeBtn.backgroundColor = [UIColor colorWithHexString:@"#FF6455"];
        self.carLineBtn.backgroundColor = [UIColor colorWithHexString:@"#FF6455"];
        self.titleLabel.textColor = [UIColor colorWithHexString:@"#FF6455"];
        if (isIol) {
            self.closeBtnLeftConstraint.priority = 250;
            self.closeBtnCenterConstraint.priority = 750;
            self.carLineBtn.hidden = YES;
        }else{
            self.closeBtnLeftConstraint.priority = 750;
            self.closeBtnCenterConstraint.priority = 250;
            self.carLineBtn.hidden = NO;
        }
    }else{
        //详情 蓝色 无车辆曲线按钮
        self.titleLabel.text = [title stringByAppendingString:@"信息详情"];
        self.closeBtn.backgroundColor = [UIColor colorWithHexString:@"#1886C8"];
        self.titleLabel.textColor = [UIColor colorWithHexString:@"#1886C8"];
        self.closeBtnLeftConstraint.priority = 250;
        self.closeBtnCenterConstraint.priority = 750;
        self.carLineBtn.hidden = YES;
    }
    [self.subTableView reloadData];
    [self show];
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    self.frame = CGRectMake(10, 74, kScreenWidth-20, kScreenHeight-84);
    [self.subTableView registerNib:[UINib nibWithNibName:YHTimeSubViewCellIdentifier bundle:nil] forCellReuseIdentifier:NSStringFromClass([YHTimeSubViewCell class])];
}

#pragma mark - table delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return globalNum1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YHTimeSubCellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YHTimeSubViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YHTimeSubViewCell class])];
    cell.isSingle = self.isSingle;
    cell.alarmDetailModel = self.list[indexPath.row];
    return cell;
}


/**关闭事件*/
- (IBAction)closeAction:(UIButton *)sender {
    [self removeFromSuperview];
    [self.maskView removeFromSuperview];
}

/**车辆曲线跳转*/
- (IBAction)goCarLineView:(UIButton *)sender {
    [self hidden];
    //    NSDate * time = [CommClass dateFormatTimeStamp:iolModel.time timeFormat:TIME_FORMATTER_24];
    //    self.timeLabel.text = [CommClass formatTimeStamp:time timeFormat:TIME_FORMATTER_HM];
    NSString *time = [self.singleTime substringWithRange:NSMakeRange(0, 16)];
    ChartsViewController *baseVC = [[ChartsViewController alloc] init];
    baseVC.startTime = time;
    baseVC.endTime = time;
    baseVC.carId = self.carId;
    baseVC.curveType = 1;//速度曲线
    [kWindow.rootViewController.childViewControllers.lastObject presentViewController:baseVC animated:YES completion:nil];
    
    if (self.maskBlock) {
        self.maskBlock();
    }
    
}

/**界面显示*/
-(void)show
{
    [kWindow addSubview:self.maskView];
    [kWindow addSubview:self];
}

#pragma mark - 背景
-(UIView *)maskView
{
    if (!_maskView) {
        _maskView = [[UIView alloc]init];
        _maskView.frame = kWindow.bounds;
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0.5;
        _maskView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden)];
        [_maskView addGestureRecognizer:tap];
    }
    return _maskView;
}
-(void)hidden
{
    [self closeAction:nil];
}
@end
